package exercise;

import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Objects;

/**
 * @classname:PassDemo
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/10 22:56
 * @version: 0.1
 * @since:1.8
 */
public class PassDemo {
    private static final String ENCODING ="UTF-8";
    public static void main(String[] args) {
//        String pass = "你真好";
//        String s = userRegister(pass);
//        System.out.println(s);
        String pass = "你真好";
        boolean result = userLogin(pass);
        System.out.println(result);


    }
    public static String userRegister(String sourcePass){
        Objects.requireNonNull(sourcePass);
        //Base64 编码 获得编码器
        Base64.Encoder encoder = Base64.getEncoder();
        //加密处理，设置编码格式ENCODING
        String string = encoder.encodeToString(sourcePass.getBytes(Charset.forName(ENCODING)));
        return string;
    }
    public static boolean userLogin(String sourecePass){
        Objects.requireNonNull(sourecePass);
        //模仿从后台数据库拿到的密码进行解密与sourecePass进行对比
        String encodePass ="5L2g55yf5aW9";
        //获得解码器
        Base64.Decoder decoder = Base64.getDecoder();
        //将密码放入解码器得到byte数组
        byte[] decode = decoder.decode(encodePass);
        //将byte数组转换成String并且配置编码格式
        String decodePass = new String(decode,Charset.forName(ENCODING));
        //将解密的String字符串和前台传来的字符串进行对比
        if(!decodePass.endsWith(sourecePass)){
            return false;
        }
        return true;
    }
}
